package com.carpedil.boot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/** @author Administrator */
@Component
public class SysConfig implements WebMvcConfigurer {
  @Autowired HandlerInterceptor myInterceptor;

  /**
   * 拦截器配置
   *
   * @param registry registry
   */
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(myInterceptor).addPathPatterns("/**").excludePathPatterns("/user/**");
  }

  /**
   * 静态资源以及虚拟路径配置
   *
   * @param registry registry
   */
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    // 配置文件上传虚拟路径
    registry.addResourceHandler("/upload/**").addResourceLocations("file:f:/upload/");
  }

  /**
   * 跨域配置,开启允许跨域
   *
   * @param registry registry
   */
  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**").allowedOrigins("*").allowedMethods("*").allowedHeaders("*");
  }

  /**
   * restTemplate 交给spring 容器
   *
   * @return RestTemplate
   */
  @Bean
  public RestTemplate restTemplate() {
    return new RestTemplate();
  }
}
